package com.kykj.kykjyzw.controller;

import com.kykj.kykjyzw.entity.ResultEntity;
import com.kykj.kykjyzw.entity.RoleEntity;
import com.kykj.kykjyzw.service.impl.RoleServiceImpl;
import com.kykj.kykjyzw.vo.RoleVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/role/")
@RestController
@CrossOrigin//解决跨域
public class RoleController {
    @Resource
    RoleServiceImpl roleService;

    @GetMapping("role")
    private ResultEntity getRoleById(@RequestParam("roleid") int roleid){
        ResultEntity resultEntity =new ResultEntity();
        try{
            RoleEntity roleEntity= roleService.getRoleById(roleid);
            if (roleEntity==null){
                resultEntity.setCode(1);
                resultEntity.setMsg("获取失败");
            }else{
                resultEntity.setCode(0);
                resultEntity.setMsg("获取成功");
                resultEntity.setData(roleEntity);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("获取失败");
        }
        return resultEntity;
    }
    @PostMapping("role")
    private  ResultEntity addRole(@RequestBody RoleVo roleVo){
        ResultEntity resultEntity =new ResultEntity();
        try{
            //后面参数不是vo对象，此处需要vo对象属性值向下传
            if (roleService.addRole(roleVo.getInfo(),roleVo.getName())>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据添加成功");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("数据添加失败");

            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据添加失败");
        }
        return resultEntity;
    }
    @PutMapping("role")
    public ResultEntity updateRole(@RequestBody RoleVo roleVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (roleService.updateRole(roleVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("数据修改成功");
            }else {
                resultEntity.setCode(1);
                resultEntity.setMsg("数据修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据修改失败");
        }
        return resultEntity;
    }
    @DeleteMapping("role/{roleid}")
    private ResultEntity delRole(@PathVariable("roleid") int roleid){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if (roleid==13){
                resultEntity.setCode(2);
                resultEntity.setMsg("默认角色不允许删除！");
            }else {
                if (roleService.delRole(roleid)>0){
                    resultEntity.setCode(0);
                    resultEntity.setMsg("数据删除成功");
                }else {
                    resultEntity.setCode(-1);
                    resultEntity.setMsg("数据删除失败");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据删除失败");
        }
        return resultEntity;
    }

    @PutMapping("changestatus")
    private ResultEntity changeRoleStatus(@RequestBody RoleVo roleVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (roleService.changeStatus(roleVo.getRoleid()) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("状态修改成功");
            } else {
                resultEntity.setCode(1);
                resultEntity.setMsg("状态修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("状态修改失败");
        }
        return resultEntity;
    }
    @GetMapping("getrolelist")
    private ResultEntity getRoleList(
            @RequestParam("name") String name,
            @RequestParam("info") String info,
            @RequestParam("page") int page,
            @RequestParam("limit") int limit
    ){
        ResultEntity resultEntity = new ResultEntity();
        int start=(page-1)*limit;
        try{
            List<RoleEntity> list = roleService.getRoleList(name,info,start,limit);
            if (name.equals("") && info.equals("")){//没有查询条件的情况下（要查询所有）
                resultEntity.setCount(roleService.getRoleCount());
            }else {
                resultEntity.setCount(list.size());//要查询条件，就返回符合条件的数量
            }
            resultEntity.setData(list);
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功");
        }catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("数据获取失败");
        }
        return  resultEntity;
    }
}
